﻿using System;
using System.Linq;
using System.Windows;
using Shoozla.ViewModel;
using Shoozla.View;
using System.IO;
using Shoozla.DataModel;
using MahApps.Metro.Controls;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Shoozla
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainView : MetroWindow
    {

        public MainView()
        {
            InitializeComponent();
            DataContext = new MainViewModel(this,null);
            this.Closing += new System.ComponentModel.CancelEventHandler(MainView_Closing);
            
        }

        void MainView_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
           // to block in case there are songs to save
            if (AppModel.IsCollectionDirty())
            {
               var decision = MetroMessageBox.MetroShow("", "Do you want to save the collection?", true, this, MessageIcon.Info,true);
               if (decision == MessageBoxResult.OK)
               {
                   e.Cancel = true;
                   ((MainViewModel)App.MainView.DataContext).CmdSave.Execute(null);
               }
            }
        }

       
        private void MetroWindow_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effects = DragDropEffects.Copy;
            else
                e.Effects = DragDropEffects.None;  
    
        }

        private void MetroWindow_DragLeave(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_DragOver(object sender, DragEventArgs e)
        {

        }

        private void MetroWindow_Drop(object sender, DragEventArgs e)
        {
            bool error = false;
            try
            {
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] ddObjects = (string[])e.Data.GetData(DataFormats.FileDrop);
                    
                    //checking if the user dropped a directory or files
                    //checking only the first element
                    if (Directory.Exists(ddObjects[0]))
                    {
                        ((MainViewModel)DataContext).DoCmdOpenFolder(new DirectoryInfo(ddObjects[0]));
                    }
                    else
                        if(File.Exists(ddObjects[0]))
                        {
                            AppModel.ResetApplication();

                            UIHelper.SetWaitCursor();

                            //the first element is a files, so start to read all the files skipping the other objects
                            List<FileInfo> ddFileInfos = new List<FileInfo>(ddObjects.Length);
                            foreach (string f in ddObjects)
                            {
                                if (File.Exists(f))
                                {
                                  //  var mp3 = AppModel.Mp3Processor.ReadID3Tag(new FileInfo(f), false);
                                    ddFileInfos.Add(new FileInfo(f));
                                }
                            }

                            //start progress bar
                            FileReader mp3TagReader = new FileReader(AppModel.MainCancellationTokenSource, AppModel.Mp3Processor);
                            mp3TagReader.Completed +=  delegate(object o, ProgressOperationEventArgs ea)
                            {
                                AppModel.ProgressOperationQueue.Remove(mp3TagReader);
                                AppModel.AddSongsToCollection(mp3TagReader.Songs);
                            };

                            var num = mp3TagReader.ReadCollection(ddFileInfos);
                            if (num > 30)
                            {
                                AppModel.ProgressOperationQueue.Add(mp3TagReader);
                                AppModel.StartProgressBarInternal(AppModel.ProgressOperationQueue.ToList<IProgressOperation>());
                            }
                            else
                                mp3TagReader.Start();
                
                          //  AppModel.AddSongsToCollection(ddMp3s);
                            AppModel.ParentFolder = null;
                            ((MainViewModel)DataContext).CurrentFolder = null;
                        }
                        else
                            error = true; 
                }
            }
            catch(Exception ex)
            {
                error = true;
            }
            finally
            {
                if(error)
                    MetroMessageBox.MetroShow("", "You can drag&drop only one Directory or Mp3 Files", true, this, MessageIcon.Warning, false);
                
                UIHelper.SetNormalCursor();
            }


        }


    }
}
